local function KKB_Cleanup_Players( UniqueIDs )
	for _,v in pairs(ents.GetAll()) do
		local UniqueID = v.KKB_OwnerID
		if ( UniqueID != nil and table.HasValue( UniqueIDs, UniqueID ) ) then
			v:Remove()
		end
	end
end

local function KKB_Cleanup_Disconnected()
	for _,v in pairs(ents.GetAll()) do
		local OwnerID = v.KKB_OwnerID
		if ( OwnerID != nil ) then
			local Player = player.GetByUniqueID( OwnerID )
			if ( not Player and not IsValid(v.KKB_DontAutoCleanup) or v.KKB_DontAutoCleanup == false ) then
				v:Remove()
			end
		end
	end
end

util.AddNetworkString( "KKB_Cleanup_Selected" )
net.Receive( "KKB_Cleanup_Selected", function(len, ply)
	if ( not ply:IsAdmin() ) then return false end
	local IDs = net.ReadTable()
	KKB_Cleanup_Players( IDs )
end )

util.AddNetworkString( "KKB_Cleanup_Disconnected" )
net.Receive( "KKB_Cleanup_Disconnected", function(len, ply)
	if ( not ply:IsAdmin() ) then return false end
	KKB_Cleanup_Disconnected()
end )

local function KKB_PlayerDisconnected( Ply )
	if ( KKB.Settings.Server.CleanupTime > 0 ) then
		local UniqueID = Ply:UniqueID()
		timer.Create( "KKB_AutoCleanup_Disconnected", KKB.Settings.Server.CleanupTime, 1, function()
			local Ply = player.GetByUniqueID( UniqueID )
			if ( not Ply ) then
				if ( KKB.Settings.Server.CleanupTime > 0 ) then
					for _,v in pairs(ents.GetAll()) do
						local UniqueID2 = v.KKB_OwnerID
						if ( UniqueID2 != nil and UniqueID2 == UniqueID ) then
							v:Remove()
						end
					end
				end
			end
		end )
	end
end
hook.Add("PlayerDisconnected", "KKB_PlayerDisconnected", KKB_PlayerDisconnected)